package com.sap.sap_mangers.controller;

import com.sap.sap_mangers.Vo.UserVo.UserMessage;
import com.sap.sap_mangers.service.DynamicService;
import com.sap.sap_mangers.service.UserService;
import com.sap.sap_mangers.until.JwtUtil;
import com.sap.sap_mangers.until.SimpleJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 动态表内容更新 controller
 *
 * @author ZhouKai
 * 2022/8/3 9:57
 */
@RestController
@RequestMapping("/Dynamic")
public class DynamicController {
    @Autowired
    DynamicService dynamicService;
    @Autowired
    private UserService userService;

    /**
     * 动态表年份信息更新
     * (年份+1； 活动次数=1； 周数=1；)
     *
     * @return 是否成功
     */
    @PreAuthorize("hasAuthority('管理员')")
    @PostMapping("/addYear")
    public SimpleJson yearUpdate() {
        UserMessage userMessage = userService.getUserMessage(JwtUtil.getUserForToken());
        if (userMessage.getRole() != 1) {
            return new SimpleJson(1, "该接口仅允许会长操作");
        }
        return dynamicService.changeYear();
    }

    /**
     * 开启新的(软协小队)活动
     * (活动次数+1； 周数=1；)
     *
     * @return 是否成功
     */
    @PreAuthorize("hasAuthority('管理员')")
    @PostMapping("/addOrder")
    public SimpleJson orderUpdate() {
        UserMessage userMessage = userService.getUserMessage(JwtUtil.getUserForToken());
        if (userMessage.getRole() != 1) {
            return new SimpleJson(1, "该接口仅允许会长操作");
        }
        return dynamicService.changeOrder();
    }

    /**
     * 当前活动的周数增加
     * (周数+1；)
     *
     * @return 是否成功
     */
    @PreAuthorize("hasAuthority('管理员')")
    @PostMapping("/addWeek")
    public SimpleJson weekUpdate() {
        UserMessage userMessage = userService.getUserMessage(JwtUtil.getUserForToken());
        if (userMessage.getRole() != 1) {
            return new SimpleJson(1, "该接口仅允许会长操作");
        }
        return dynamicService.changeWeek();
    }

    @PreAuthorize("hasAuthority('会长')")
    @GetMapping("/getNow")
    public SimpleJson getNow() {
        UserMessage userMessage = userService.getUserMessage(JwtUtil.getUserForToken());
        if (userMessage.getRole() != 1) {
            return new SimpleJson(1, "该接口仅允许会长操作");
        }
        return new SimpleJson(0, "获取成功", dynamicService.getYear() + "-" + dynamicService.getOrder() + "-" + dynamicService.getWeek());
    }

    @PreAuthorize("hasAuthority('会长')")
    @PostMapping("/reduceWeek")
    public SimpleJson reduceWeek() {
        UserMessage userMessage = userService.getUserMessage(JwtUtil.getUserForToken());
        if (userMessage.getRole() != 1) {
            return new SimpleJson(1, "该接口仅允许会长操作");
        }
        return dynamicService.reduceWeek();
    }

    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getHighestOnlinePeople")
    public SimpleJson getHighestOnlinePeople() {
        return new SimpleJson(0, dynamicService.getHighestOnlinePeople());
    }

}
